﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClientWPF.localhost;

namespace ClientWPF
{
    public partial class BuyProductForm : Window
    {
        Service1 service = new Service1();
        Product product = new Product();
        AuthentificationCredentials authentification = new AuthentificationCredentials();

        public BuyProductForm()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

        private void CheckPriceButton_Click(object sender, RoutedEventArgs e)
        {
            Product product = new Product();
            Product _product = new Product();
            _product.Name = NameBox.Text;
            if (!service.CheckProductExist(_product))
            {
                MessageBox.Show("Product " + _product.Name + " don't exist!");
            }
            else
            {
                product = _product;
                double price = service.GetPrice(product);
                int quantity = 0;
                try
                {
                    quantity = Convert.ToInt32(QuantityBox.Text);
                }
                catch (Exception a)
                {
                    MessageBox.Show("Price error message:\n" + a.ToString());
                }
                
                /*if (Convert.ToInt32(QuantityBox.Text).Equals(0)==false)
                {
                    quantity = Convert.ToInt32(QuantityBox.Text);
                }*/
                //int quantity = Convert.ToInt32(QuantityBox.Text);
                string priceprod = Convert.ToString(price * quantity);
                PriceBox.Text = priceprod;
            }
            

        }

        private void BuyProductButton_Click(object sender, RoutedEventArgs e)
        {
            authentification.Privileges = "user";
            service.AuthentificationCredentialsValue = authentification;
            Product _product = new Product();
            _product.Name = NameBox.Text;
            if (!service.CheckProductExist(_product))
            {
                MessageBox.Show("Product " + _product.Name + " don't exist!");
            }
            else
            {
                product = _product;
                try
                {
                    product.Quantity = Convert.ToInt32(QuantityBox.Text);
                }
                catch (Exception a)
                {
                    MessageBox.Show("Quantity error message:\n" + a.ToString());
                }
                //product.Quantity = Convert.ToInt32(QuantityBox.Text);
                if (service.CheckProductQuantity(product))
                {
                    service.BuyProduct(product);
                    Close();
                }
                else MessageBox.Show("Not enough products in the warehouse!");
            }
            
        }

        

        
    }
}
